package com.elder.business.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.ElderlyInfo;
import com.elder.business.domain.HealthCheckRecords;
import com.elder.business.domain.HealthCheckRecordsDTO;
import com.elder.business.domain.MedicationRecords;
import com.elder.business.mapper.ElderlyInfoMapper;
import com.elder.business.mapper.HealthCheckRecordsMapper;
import com.elder.business.service.IHealthCheckRecordsService;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.enums.CheckUpType;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.StringNewUtils;
import com.elder.common.utils.bean.BeanUtils;
import com.elder.common.utils.uuid.SnowGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author elder
 * @date 2024-04-11
 */
@Service
public class HealthCheckRecordsServiceImpl implements IHealthCheckRecordsService
{
    @Autowired
    private HealthCheckRecordsMapper healthCheckRecordsMapper;
    @Autowired
    private ElderlyInfoMapper elderlyInfoMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public JSONObject selectHealthCheckRecordsById(Long id)
    {
        HealthCheckRecords v = healthCheckRecordsMapper.selectHealthCheckRecordsById(id);
        ElderlyInfo elderlyInfo = Objects.isNull(v.getElderId()) ? new ElderlyInfo() : elderlyInfoMapper.getElderInfoByElderId(v.getElderId());
        JSONObject obj = new JSONObject();
        obj.put("healthCheckId", StringNewUtils.formatStr(v.getId()));
        obj.put("elderId", StringNewUtils.formatStr(v.getElderId()));
        obj.put("elderName", StringNewUtils.isEmpty(elderlyInfo.getElderName())?"":elderlyInfo.getElderName());
        obj.put("checkupName", v.getCheckupName());
        obj.put("checkupTime", DateUtils.dateStrMIAO(v.getCheckupTime()));
        obj.put("checkupType", CheckUpType.getCnName(v.getCheckupType()));
        obj.put("checkupResult", v.getCheckupResult());
        obj.put("remark", v.getRemark());
        return obj;
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param healthCheckRecords 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<HealthCheckRecords> selectHealthCheckRecordsList(HealthCheckRecords healthCheckRecords)
    {
        return healthCheckRecordsMapper.selectHealthCheckRecordsList(healthCheckRecords);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param healthCheckDTO 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertHealthCheckRecords(HealthCheckRecordsDTO healthCheckDTO)
    {
        HealthCheckRecords healthCheckRecords = new HealthCheckRecords();
        BeanUtils.copyProperties(healthCheckDTO,healthCheckRecords);
        healthCheckRecords.setId(SnowGenerator.getId());
        healthCheckRecords.setCheckupType(CheckUpType.getValue(healthCheckDTO.getCheckupType()));
        healthCheckRecords.setCheckupTime(DateUtils.getDateSeconds(healthCheckDTO.getCheckupTime()));
        healthCheckRecords.setCreateTime(DateUtils.getLongTime());
        healthCheckRecords.setUpdateTime(DateUtils.getLongTime());
        return healthCheckRecordsMapper.insertHealthCheckRecords(healthCheckRecords);
    }

    /**
     * 修改
     * 
     * @param healthCheckDTO
     * @return 结果
     */
    @Override
    public int updateHealthCheckRecords(HealthCheckRecordsDTO healthCheckDTO)
    {
        HealthCheckRecords healthCheckRecords = new HealthCheckRecords();
        BeanUtils.copyProperties(healthCheckDTO,healthCheckRecords);
        healthCheckRecords.setCheckupType(CheckUpType.getValue(healthCheckDTO.getCheckupType()));
        healthCheckRecords.setCheckupTime(DateUtils.getDateSeconds(healthCheckDTO.getCheckupTime()));
        healthCheckRecords.setUpdateTime(DateUtils.getLongTime());
        return healthCheckRecordsMapper.updateHealthCheckRecords(healthCheckRecords);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteHealthCheckRecordsByIds(Long[] ids)
    {
        return healthCheckRecordsMapper.deleteHealthCheckRecordsByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteHealthCheckRecordsById(Long id)
    {
        return healthCheckRecordsMapper.deleteHealthCheckRecordsById(id);
    }

    @Override
    public TableDataInfo getHealthCheckRecordsList(String elderName, String checkupName,String checkupType, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        List<Long> elderIds = null;
        if(!StringNewUtils.isEmpty(elderName)){
            List<ElderlyInfo> elderList = elderlyInfoMapper.getElderList(elderName);
            if(CollectionUtils.isNotEmpty(elderList)){
                elderIds = elderList.stream().map(ElderlyInfo::getElderId).collect(Collectors.toList());
            }
        }
        //时间处理
        Long daystartTime = null;
        Long dayendTime = null;
        if(!StringNewUtils.isEmpty(startTime)&& !StringNewUtils.isEmpty(endTime)){
            Long currentTime = DateUtils.getTargetDateSeconds(startTime);
            Long endCurrentTime = DateUtils.getTargetDateSeconds(endTime);
            daystartTime = DateUtils.getDayStartTime(currentTime);
            dayendTime = DateUtils.getDayEndTime(endCurrentTime);
        }
        Integer checkupTypeInt = null;
        if(!StringNewUtils.isEmpty(checkupType)){
            checkupTypeInt = CheckUpType.getValue(checkupType);
        }
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<HealthCheckRecords> list = healthCheckRecordsMapper.getHealthCheckRecordsList(elderIds,checkupName,checkupTypeInt,daystartTime,dayendTime);
        PageInfo<HealthCheckRecords> pageInfo = new PageInfo<>(list);
        List<HealthCheckRecords> healthCheckRecordsList = pageInfo.getList();
        List<Long> elderIdList = healthCheckRecordsList.stream().filter(rel -> Objects.nonNull(rel.getElderId())).map(HealthCheckRecords::getElderId).collect(Collectors.toList());
        Map<Long,String> elderMap = CollectionUtils.isEmpty(elderIdList)? new HashMap<>():
                elderlyInfoMapper.getElderInfoByElderIds(elderIdList).stream().collect(Collectors.toMap(ElderlyInfo::getElderId, ElderlyInfo::getElderName));
        List<JSONObject> jsonObjects = healthCheckRecordsList.stream().map(v -> {
            JSONObject obj = new JSONObject();
            obj.put("healthCheckId", StringNewUtils.formatStr(v.getId()));
            obj.put("elderId", StringNewUtils.formatStr(v.getElderId()));
            obj.put("elderName", Objects.isNull(elderMap.get(v.getElderId()))?"":elderMap.get(v.getElderId()));
            obj.put("checkupName", v.getCheckupName());
            obj.put("checkupTime", DateUtils.dateStrMIAO(v.getCheckupTime()));
            obj.put("checkupType", CheckUpType.getCnName(v.getCheckupType()));
            obj.put("checkupResult", v.getCheckupResult());
            obj.put("remark", v.getRemark());
            return obj;
        }).collect(Collectors.toList());
        TableDataInfo result = new TableDataInfo();
        result.setCode(200);
        result.setTotal(pageInfo.getTotal());
        result.setRows(jsonObjects);
        return result;
    }
}
